import CryptoJS from 'crypto-js'
import qs from 'qs'

export const encrypt = {
  auth: (request) => {
    let params = null
    const currentUser = JSON.parse(window.sessionStorage.getItem('loginUser'))
    const token = currentUser.token
    const secret = currentUser.secret
    const timeStamp = Date.now()
    const once = Math.random().toString(16)
    switch (request.method) {
      case 'get':
      case 'delete':
        if (request.url.indexOf('?') > -1) {
          const parameters = request.url.split('?')
          params = encrypt.objKeySort(qs.parse(parameters[1]))
        }
        if (request.params) {
          if (params === null) {
            params = encrypt.objKeySort(request.params)
          } else {
            params = params + encrypt.objKeySort(request.params)
          }
        }
        break
      case 'post':
      case 'put':
        params = JSON.stringify(request.data)
        break
    }
    let originSign = null
    if (params === null) {
      originSign = token + timeStamp + once + secret
    } else {
      originSign = token + timeStamp + once + params + secret
    }
    const sign = CryptoJS.MD5(originSign).toString().toUpperCase()
    return (
      CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(token)) +
      '.' +
      CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(timeStamp)) +
      '.' +
      CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(once)) +
      '.' +
      CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(sign))
    )
  },
  selfAuth: (request, config) => {
    let params = null
    const timeStamp = Date.now()
    const type = 'md5'
    const secret = config.secret
    const token = JSON.stringify({
      userId: config.userId,
      systemId: config.systemId,
      deptId: config.deptId
    })
    switch (request.method) {
      case 'get':
      case 'delete':
        if (request.url.indexOf('?') > -1) {
          const parameters = request.url.split('?')
          params = encrypt.objKeySort(qs.parse(parameters[1]))
        }
        if (request.params) {
          if (params === null) {
            params = encrypt.objKeySort(request.params)
          } else {
            params = params + encrypt.objKeySort(request.params)
          }
        }
        break
      case 'post':
      case 'put':
        params = JSON.stringify(request.data)
        break
    }
    let originSign = null
    if (params === null) {
      originSign = type + '.' + timeStamp + '.' + token + '.' + secret
    } else {
      originSign = type + '.' + timeStamp + '.' + token + '.' + params + '.' + secret
    }
    const sign = CryptoJS.MD5(originSign).toString().toUpperCase()
    return (
      CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(type)) +
      '.' +
      CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(timeStamp)) +
      '.' +
      CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(token)) +
      '.' +
      CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(sign))
    )
  },
  objKeySort: (params) => {
    const newKeys = Object.keys(params).sort()
    let newParams = null
    for (let i = 0; i < newKeys.length; i++) {
      if (
        params[newKeys[i]] === null ||
        params[newKeys[i]] === '' ||
        typeof params[newKeys[i]] === 'undefined' ||
        (typeof params[newKeys[i]] === 'object' && params[newKeys[i]].length === 0)
      ) {
        continue
      }
      if (newParams === null) {
        newParams = newKeys[i] + params[newKeys[i]]
        continue
      }
      newParams += newKeys[i] + params[newKeys[i]]
    }
    return newParams
  }
}
